<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="参数" prop="parameter">
        <el-input
          v-model="queryParams.parameter"
          placeholder="请输入参数"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button  @click="addData">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column v-if="false" label="参数ID" prop="listId"></el-table-column>
      <el-table-column label="参数" prop="code"></el-table-column>
      <el-table-column label="参数说明" prop="txt"></el-table-column>
      <el-table-column label="参数类型" prop="type"  :formatter="typeFormatter"></el-table-column>
      <el-table-column label="参数值类型" prop="valueType" :formatter="valueTypeFormatter">
      </el-table-column>
      <el-table-column label="参数码表" prop="sourceTable"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row, 'edit')" ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" :title="handleType ==='edit'?'修改参数配置': '添加参数'" width="500">
      <el-form :model="dialogForm">
        <el-form-item v-show='false' label="参数" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.listId" />
        </el-form-item>
        <el-form-item label="参数" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.txt" autocomplete="off" />
        </el-form-item>
        <el-form-item label="参数说明" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="参数类型" :label-width="formLabelWidth">
          <el-select
            v-model="dialogForm.type"
            placeholder="请选择"
            clearable
          >
            <el-option key="" label="请选择" value="" />
            <el-option key="1" label="输入型参数" value="1" />
            <el-option key="2" label="配置型参数" value="2" />
            <el-option key="3" label="计算型参数" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="参数相关性" :label-width="formLabelWidth">
          <el-select 
            v-model="dialogForm.valueType"
            placeholder="请选择"
            clearable
          >
            <el-option v-for="item in valueTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="参数码表" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.sourceTable" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Parameter">
import { parameterQuery, parameterSave, parameterDelete } from '@/api/safeguard/index';
import { rowContextKey } from 'element-plus';

const queryParams = ref({
  parameter: ''
})

const showSearch = ref(true);
const loading = ref(false);

const handleType = ref('')
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const dialogForm = ref({
  listId: '',
  txt: '', // 参数
  code: '', // 参数编码
  type: '', // 参数类型
  valueType: '', // 参数值类型
  sourceTable: '', // 参数码表
})
const valueTypeOptions = ref([
  {value: 'number',label: '数字'},
  {value: 'single_line_text',label: '文本'},
  {value: 'date',label: '日期'},
  {value: 'single_check',label: '码表'},
])
const tableData = ref([]);
handleQuery();
  /** 查询 */
function handleQuery() {
  getList();
}

function getList () {
  loading.value = true;
  parameterQuery('111').then(res => {
    tableData.value = res.data || []
    //total.value = res.total || 0
  }).finally(() => {
    loading.value = false;
  })
}

/** 添加 */
function addData () {
  dialogFormVisible.value = true
  dialogForm.value = {}
}
 /** 编辑 */
function handleUpdate (row, type) {
  handleType.value = type
  dialogForm.value = row
  dialogFormVisible.value = true
}

function handleSave() {  
  // 保存成功刷新数据
  parameterSave({...dialogForm.value}).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}

function handleDelete(row){
    parameterDelete(row.listId).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}

function valueTypeFormatter(row){
  const item = valueTypeOptions.value.find(item => item.value == row.valueType)
  return item ? item.label : '';
  //return row.valueType.replace("number", "数字");
}

function typeFormatter(row){
  return row.type.replace("1", "输入型");
}
</script>
